package com.wyt.service.impl;

import com.wyt.dao.mapper.AbilitiesMapper;
import com.wyt.dao.mapper.LikesMapper;
import com.wyt.dao.mapper.ServicesMapper;
import com.wyt.pojo.Abilities;
import com.wyt.pojo.AbilitiesVO;
import com.wyt.pojo.ResServicesVO;
import com.wyt.pojo.Services;
import com.wyt.service.ResContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ResContentImpl implements ResContent {

    @Autowired
    private ServicesMapper servicesMapper;

    @Autowired
    private AbilitiesMapper abilitiesMapper;

    @Autowired
    private LikesMapper likesMapper;


    @Override
    public List<ResServicesVO> findComment() {
        // 全部的服务
        List<Services> allServices = servicesMapper.findAllServices();
        // 返回内容：内容条数等于服务条数
        ArrayList<ResServicesVO> resContents = new ArrayList<>(allServices.size());

        // 对于每一条返回的内容
        for(int i=0; i<allServices.size();i++) {
            ResServicesVO resServicesVO = new ResServicesVO();

            // 获得服务id
            int sid = allServices.get(i).getServiceId();
            // 获得服务名称
            String sname = allServices.get(i).getServiceName();
            // 获得服务i的全部能力列表
            List<Abilities> abilities = abilitiesMapper.queryAbilitiesList(sid);
            // 建立服务i的带有点赞数的全部能力列表
            List<AbilitiesVO> abilitiesVOS = new ArrayList<AbilitiesVO>(abilities.size());

            // 将每个能力的点赞数加入到能力中，Abilities--> AbilitiesVO
            for (int j=0; j<abilities.size(); j++){
                AbilitiesVO a = new AbilitiesVO();
                // 基本信息从能力表复制过来即可
                a.setAbilityId(abilities.get(j).getAbilityId());
                a.setAbilityName(abilities.get(j).getAbilityName());
                a.setAbilityInfo(abilities.get(j).getAbilityInfo());
                a.setAbilityValue(abilities.get(j).getAbilityValue());
                a.setAbilityEnv(abilities.get(j).getAbilityEnv());
                // 点赞数从likesMapper得到
                int count = likesMapper.countLikes(abilities.get(j).getAbilityId());
                a.setLikesCount(count);
                abilitiesVOS.add(a);
            }

            // 返回的数据如下：
            resServicesVO.setServiceId(sid); // 服务id
            resServicesVO.setServiceName(sname);  // 服务名称
            resServicesVO.setAbilitiesVOList(abilitiesVOS); // 带有点赞数的服务能力列表

            resContents.add(resServicesVO);
        }
        return resContents;
    }
}
